import { queryAll, remove, query ,create,update,invalid, deleteBatch } from '../services/customerInforDetails';
import { message } from 'antd';

export default {

  namespace: 'customerInforDetail',

  state: {
    load : true,
    list: [],
    curItem: {},
    modalVisible: false,
    modalType: 'create',
    record:[],
    selectedRowKeys:[],
    index:-1,

  },

  subscriptions: {
    setup({ dispatch, history }) {
      history.listen(location => {
        const pathname = location.pathname
        if(pathname === '/customerInforDetail'){
          dispatch({
            type: 'query',
            payload: location.query,
          })
        }
      })
    },
  },

  effects: {
    *fetch({ payload }, { call, put }) {  // eslint-disable-line
      yield put({ type: 'save' });
    },
    *query({ payload }, { call, put }){
      const data = yield call(queryAll)
      //console.log('Model:',data);
      yield put ({type: 'hideLoading'})
      if(data){
        yield put({
          type: 'querySuccess',
          payload: {
            list: data,
          },
        })
      }
    },

    *create ({ payload },{ call, put }){
       const item=payload;
       //console.log(item);
       const data=yield call(create, {item})
       yield put({type:'hideLoading'})
       if(data){
         yield put({
           type:'querySuccess',
             payload:{
               list:data,
             }
         })
       }
    },

    *'delete' ({ payload }, { call, put }) {
       const data = yield call(remove, { payload })
       if(data){
         yield put({
           type: 'querySuccess',
           payload: {
             list: data,
           },
         })
         message.success('删除成功！',1)
       }
     },

  *update ({ payload }, { select, call, put }) {

    const id = yield select(({ customerInforDetail }) => customerInforDetail.curItem.id)
    const newCustomer = { ...payload, id }
    const data = yield call(update, newCustomer)
    if (data) {
    yield put({type: 'hideModal'})
    yield put({ type: 'query'})
    message.success('修改成功！',1)
  } else {
    throw data
    }
  },
  *search({ payload }, {call, put ,select}){
    const {customerName, customerNum, customerEname, customerAbbreviation,
            customerProperty, customerType, industryCategory, isServing} = payload;
    const list = yield call(queryAll);
    //console.log('model>>>>>',customerProperty);
    let data = list;

       if (isBadRecord) {
         data = data.filter((item)=>item.isBadRecord===isBadRecord[0]);
       }

    if(data){
      yield put({
        type: 'querySuccess',
        payload: {
          list: data,
        },
      })
    }
  }

  },

  reducers: {
    save(state, action) {
      return { ...state, ...action.payload };
    },

    querySuccess (state, action) {
      const { list } = action.payload
      return { ...state,
        list,
       }
    },

    showModal (state, { payload }) {
      return { ...state, ...payload, modalVisible: true }
    },

    hideModal (state) {
      return { ...state, modalVisible: false }
    },
    //隐藏加载
    hideLoading (state,action) {
      return {
        load: false
      }
    },
  },
};
